package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.utils.DensityUtil;

/**
 * Created by tangbingliang on 17/2/16.
 */

public class WaitingDialog extends MyBaseDialog {
    private Context context;
    private ImageView imageView;
    private AnimationDrawable anim;
    private View view;
    private TextView textView;

    public static final int STATUS_ING = 1;
    public static final int STATUS_FINISH = 2;
    public static final int STATUS_ERROR = 3;
    public static final int STATUS_NET_ERROR = 4;

    /*
     * private ImageView anim_img; private TextView content_tv;
     */
    public WaitingDialog(Context context, String str) {
        super(context, R.style.DT_DIALOG_Translucent);
        this.context = context;

        if (str.isEmpty()) {
            str = context.getResources().getString(R.string.load_ing);
        }

        view = LayoutInflater.from(context).inflate(
                R.layout.dt_now_page_loading, null);
        imageView = (ImageView) view.findViewById(R.id.loding_img);
        textView = (TextView) view.findViewById(R.id.content_tv);
        anim = (AnimationDrawable) context.getResources().getDrawable(
                R.drawable.loading_drawable);
        imageView.setImageDrawable(anim);
        textView.setText(str);
        super.createView();
        super.setCancelable(true);
        super.setCanceledOnTouchOutside(true);
        setWindowBgAlpha(0);
        int width = DensityUtil.dp2px(context, 60);
        setWindowSize(width, width);
    }

    public void changeStatusStr(String str) {
        textView.setText(str);
    }

    public void changeStatus(int status) {
        switch (status) {
            case STATUS_ING:
                textView.setText(context.getResources().getString(R.string.load_ing));
                imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.loading_drawable));
                break;
            case STATUS_FINISH:
                textView.setText(context.getResources().getString(R.string.load_finish));
                imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
                break;
            case STATUS_ERROR:
                textView.setText(context.getResources().getString(R.string.load_error));
                imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
                break;
            case STATUS_NET_ERROR:
                textView.setText(context.getResources().getString(R.string.load_net_error));
                imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
                break;
        }
    }

    @Override
    protected View getView() {
        // imageView.setImageResource(R.drawable.loading_drawable);
		/*
		 * anim_img = (ImageView) view.findViewById(R.id.anim_img); content_tv =
		 * (TextView) view.findViewById(R.id.content_tv);
		 */
        return view;
    }

    public void startAnimation() {
        if (!anim.isRunning()) {
            anim.start();
        }

    }

    public void stopAnimation() {
        if (anim.isRunning()) {
            anim.stop();
        }
    }

    public ImageView getAnim_img() {
        return imageView;
    }

    public TextView getContent_tv() {
        return new TextView(context);
    }
}
